package com.fjec.market.schedule.handler;

import com.fjec.common.schedule.BaseScheduleHandler;
import com.fjec.common.util.JsonUtil;
import com.fjec.market.mapper.InfoBusinessMapper;
import com.fjec.market.mapper.InfoFarmerMapper;
import com.fjec.market.mapper.InfoPurchaseMapper;
import com.fjec.market.mapper.InfoSkuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class TmpUpdateShopWeightHandler extends BaseScheduleHandler {

    @Resource
    private InfoFarmerMapper farmerMapper;

    @Resource
    private InfoBusinessMapper businessMapper;

    @Resource
    private InfoPurchaseMapper purchaseMapper;

    @Resource
    private InfoSkuMapper skuMapper;

    /**
     * 临时定时更新店铺排序
     *
     * @param num
     * @return
     */
    public String handle(int num) {
        List<Map<String, Object>> errorList = new ArrayList<>();
        Long fn = 0L;
        Long bn = 0L;
        Long pn = 0L;
        Long skuNum = 0L;
        // 2
        try {
            fn = farmerMapper.tmpUpdateWeight(num);
        } catch (Exception ex) {
            String guid = this.error(ex);
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("guid", guid);
            errorMap.put("type", "farmer");
            errorList.add(errorMap);
        }
        // 3 4 5
        try {
            bn = businessMapper.tmpUpdateWeight(num);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            String guid = this.error(ex);
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("guid", guid);
            errorMap.put("type", "business");
            errorList.add(errorMap);
        }
        // 6
        try {
            pn = purchaseMapper.tmpUpdateWeight(num);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            String guid = this.error(ex);
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("guid", guid);
            errorMap.put("type", "purchase");
            errorList.add(errorMap);
        }
        // sku
        try {
            skuNum = skuMapper.tmpUpdateSaleNum();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            String guid = this.error(ex);
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("guid", guid);
            errorMap.put("type", "sku");
            errorList.add(errorMap);
        }
        String result;
        if (errorList.isEmpty()) {
            // 全部处理，无异常
            result = String.format("FARMER: [%s], BUSINESS: [%s], PURCHASE: [%s], SKU: [%s]",
                    fn, bn, pn, skuNum);
        } else {
            result = String.format("Temp weight handler error num: [%s], %s",
                    errorList.size(), JsonUtil.toJson(errorList));
        }
        return result;
    }
}
